package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.common.exception.AppException;
import com.future.dao.KinderDao;
import com.future.model.Kginfo;
import com.future.requestbean.KgInfo;
import com.future.requestbean.KgInfoUpdate;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/kinder")
public class KinderController extends BaseController {
	@Inject
	private KinderDao kinderDao;

	public void getkinderInfo() {
		renderResult("kinderinfo", kinderDao.findAll());
	}

	@Before(Tx.class)
	public void addKinderInfo() {
		int kindersize = this.kinderDao.findAll().size();
		if (kindersize > 0) {
			throw new AppException("幼儿园信息只能有一条");
		}
		KgInfo kgInfo = getJsonBody(KgInfo.class);
		Kginfo kg = beanToModel(kgInfo, Kginfo.class);
		this.kinderDao.save(kg);
		renderSuccess();
	}

	@Before(Tx.class)
	public void deleteKinderInfo(@NotNull(message = "选择删除对象后再删除") Long kgid) {
		this.kinderDao.deleteById(kgid);
		renderSuccess();
	}

	@Before(Tx.class)
	public void updateKinderInfo() {
		Kginfo kginfo = getJsonBodyToModel(KgInfoUpdate.class, Kginfo.class);
		this.kinderDao.update(kginfo);
		renderSuccess();
	}
}
